package com.woniuxy.classteacher.service.impl;

import com.woniuxy.classteacher.mapper.TeacherMapper;
import com.woniuxy.classteacher.mapper.UserMapper;
import com.woniuxy.classteacher.mqsend.CodeSend;
import com.woniuxy.classteacher.service.TeacherService;
import com.woniuxy.commons.entity.wyh.TeacherInfo;
import com.woniuxy.commons.exception.AffirmPwdException;
import com.woniuxy.commons.exception.CodeException;
import com.woniuxy.commons.exception.OriginPwdNotMatchingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Auther: 牟强
 * @Date: 2020/8/1/001 10:18
 * @Description:
 */
@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CodeSend codeSend;
    @Autowired
    private UserMapper userMapper;

    /**
     * @Author: 牟强
     * @Date: 2020/8/1/001 15:20
     * @Description: 生成4位随机数
     * @Param: []
     * @return: java.lang.String
     */
    public String generateShortUuid() {
        int num = (int) (Math.random() * 10000);
        return num + "";
    }

    /**
     * @Author: 牟强
     * @Date: 2020/8/5/005 16:40
     * @Description: 修改密码
     * @Param: [originPwd, newPwd, affirmPwd, code, rId]
     * @return: void
     */
    @Transactional
    @Override
    public void alterTeacherPwd(String originPwd, String newPwd, String affirmPwd, String code, int rId) {
        TeacherInfo teacherInfo = teacherMapper.findTeacherByTId(rId);
        //比较原密码
        if (!teacherInfo.getPassword().equals(originPwd)) {
            throw new OriginPwdNotMatchingException("原密码不匹配");
        }
        //确认密码校验
        if (!newPwd.equals(affirmPwd)) {
            throw new AffirmPwdException("确认密码与新密码不一致");
        }
//        //校验手机短信验证码             该项目经费不足故放弃该功能
//        String code1 = (String) redisTemplate.opsForValue().get(teacherInfo.getAccount());
//        if (code1 == null || !code1.equals(code)) {
//            throw new CodeException("验证码不正确");
//        }
        //修改密码
        teacherMapper.updateTeacherPwdByTId(teacherInfo.getTcId(), newPwd);
        //根据账号和角色id将新密码同步到user表中
        userMapper.updatePasswordByAccountAndRoleId(teacherInfo.getAccount(), teacherInfo.getTcId(), newPwd);
    }

    /**
     * @Author: 牟强
     * @Date: 2020/8/5/005 16:40
     * @Description: 发送验证码
     * @Param: [rId]
     * @return: void
     */
    @Override
    public void sendAuthCode(int rId) {
        TeacherInfo teacherInfo = teacherMapper.findTeacherByTId(rId);
        //验证码
        String code = generateShortUuid();
        //发送手机号和验证码
        Map<String, String> map = new HashMap<>();
        map.put("phone", teacherInfo.getTel());
        map.put("code", code);
        codeSend.send(map);
        //将验证码存入redis 参数一：key   参数二：value   参数三：时间  参数四：单位
        redisTemplate.opsForValue().set(teacherInfo.getAccount(), code, 5, TimeUnit.MINUTES);
    }

    /**
     * @Author: 牟强
     * @Date: 2020/8/5/005 16:40
     * @Description: 初始化修改密码界面信息
     * @Param: [refreshToken]
     * @return: com.woniuxy.commons.entity.wyh.TeacherInfo
     */
    @Override
    public TeacherInfo initUptatePwd(String refreshToken) {
        Map map = redisTemplate.opsForHash().entries(refreshToken);
        Integer rId = (Integer) map.get("rid");
        TeacherInfo teacherInfo = teacherMapper.findTeacherByTId(rId);
        return teacherInfo;
    }

}
